package com.platform.system.service.impl;


import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.constant.Constants;
import com.platform.common.constant.HttpStatus;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.domain.entity.SysUser;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.bean.BeanUtils;
import com.platform.common.utils.uuid.SnowflakeIdWorker;
import com.platform.system.domain.dto.device.TenantAuthAiDto;
import com.platform.system.domain.entity.chip.Tenant;
import com.platform.system.domain.entity.device.TenantAuthAi;
import com.platform.system.domain.vo.device.TenantAuthAiVo;
import com.platform.system.logic.TenantAuthAiLogic;
import com.platform.system.mapper.device.TenantAuthAiMapper;
import com.platform.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Huangkai
 */
@Slf4j
@Service
public class SysTenantAuthAiServiceImpl extends ServiceImpl<TenantAuthAiMapper, TenantAuthAi> implements ISysTenantAuthAiService {

    @Resource
    TenantAuthAiMapper tenantAuthAiMapper;

    @Resource
    ISysTenantAuthAiService sysTenantAuthAiService;

    @Autowired
    ISysTenantService sysTenantService;

    @Resource
    TenantAuthAiLogic tenantAuthAiLogic;

    @Resource
    ISysAiUiConfigService sysAiUiConfigService;

    @Override
    public TableDataInfo selectTenantAuthAiList(TenantAuthAiVo tenantAuthAiVo) {
        LambdaQueryWrapper<TenantAuthAi> tenantAuthAiLambdaQueryWrapper = new QueryWrapper<TenantAuthAi>().lambda();
        tenantAuthAiLambdaQueryWrapper.orderByDesc(TenantAuthAi::getCreateTime);
        Page<TenantAuthAi> page = new Page<>();
        page.setCurrent(tenantAuthAiVo.getPageNum());
        page.setSize(tenantAuthAiVo.getPageSize());
        IPage<TenantAuthAi> tenantAuthAiIPage = tenantAuthAiMapper.selectPage(page, tenantAuthAiLambdaQueryWrapper);
        TableDataInfo rspData = new TableDataInfo();
        List<TenantAuthAi> list =  tenantAuthAiIPage.getRecords();
        List<TenantAuthAiDto> tenantAuthAiDtoList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            log.info("数量{}", list);
            String array = JSONArray.toJSONString(list);
            tenantAuthAiDtoList = JSONArray.parseArray(array, TenantAuthAiDto.class);
            List<String> tenantUidList = tenantAuthAiDtoList.stream().map(TenantAuthAiDto::getTenantUid).collect(Collectors.toList());
            List<Tenant>  tenantList = sysTenantService.selectTenantNameListByUid(tenantUidList);
            Map<String, Tenant> sysTenantMap = tenantList.stream().collect(Collectors.toMap(Tenant::getUid, Function.identity()));
            tenantAuthAiDtoList.forEach(tenantAuthAiDto -> {
                if(tenantAuthAiDto.getTenantUid().equals(Constants.STR_ZERO)){
                    tenantAuthAiDto.setTenantName("鱼亮科技有限公司");
                }else if(sysTenantMap.containsKey(tenantAuthAiDto.getTenantUid())){
                    Tenant tenant = sysTenantMap.get(tenantAuthAiDto.getTenantUid());
                    tenantAuthAiDto.setTenantName(tenant.getName());
                }
                if(tenantAuthAiDto.getIsAll().equals(Constants.NUM_ZERO)){
                    tenantAuthAiLogic.buildTenantAuthAi(tenantAuthAiDto);
                }
            });
        }
        rspData.setRows(tenantAuthAiDtoList);
        rspData.setTotal(tenantAuthAiIPage.getTotal());
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        return rspData;
    }

    @Override
    public AjaxResult insertTenantAuthAi(TenantAuthAiVo tenantAuthAiVo) {
        LambdaQueryWrapper<TenantAuthAi> tenantAuthAiLambdaQueryWrapper = new QueryWrapper<TenantAuthAi>().lambda();
        tenantAuthAiLambdaQueryWrapper.eq(TenantAuthAi::getTenantUid, tenantAuthAiVo.getTenantUid());
        TenantAuthAi tenantAuthAi = tenantAuthAiMapper.selectOne(tenantAuthAiLambdaQueryWrapper);
        if(tenantAuthAi != null){
            throw new BusinessException(ResultEnum.SETTING_TENANT);
        }
        tenantAuthAi = new TenantAuthAi();
        tenantAuthAi.setUid(SnowflakeIdWorker.getSnowflakeIdWorker());
        tenantAuthAi.setTenantUid(tenantAuthAiVo.getTenantUid());
        tenantAuthAi.setAiuiUidList(JSONArray.toJSONString(tenantAuthAiVo.getAiuiUidList()));
        tenantAuthAi.setModelUidList(JSONArray.toJSONString(tenantAuthAiVo.getModelUidList()));
        tenantAuthAi.setHumanUidList(JSONArray.toJSONString(tenantAuthAiVo.getHumanUidList()));
        tenantAuthAi.setCreateTime(new Date());
        tenantAuthAi.setUpdateTime(new Date());
        tenantAuthAi.insert();
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateTenantAuthAi(TenantAuthAiVo tenantAuthAiVo) {
        log.info("修改的信息{}", tenantAuthAiVo);
        TenantAuthAiDto tenantAuthAiDto = sysTenantAuthAiService.selectTenantAuthAiByTenantUid(tenantAuthAiVo);
        if(tenantAuthAiDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<TenantAuthAi> tenantAuthAiLambdaQueryWrapper = new QueryWrapper<TenantAuthAi>().lambda();
        tenantAuthAiLambdaQueryWrapper.eq(TenantAuthAi::getTenantUid, tenantAuthAiVo.getTenantUid());
        TenantAuthAi tenantAuthAi = new TenantAuthAi();
        BeanUtils.copyProperties(tenantAuthAiVo, tenantAuthAi);
        tenantAuthAi.setUpdateTime(new Date());
        tenantAuthAi.update(tenantAuthAiLambdaQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public TenantAuthAiDto selectTenantAuthAiByTenantUid(TenantAuthAiVo tenantAuthAiVo) {
        LambdaQueryWrapper<TenantAuthAi> tenantAuthAiLambdaQueryWrapper = new QueryWrapper<TenantAuthAi>().lambda();
        tenantAuthAiLambdaQueryWrapper.eq(TenantAuthAi::getTenantUid, tenantAuthAiVo.getTenantUid());
        TenantAuthAi tenantAuthAi = tenantAuthAiMapper.selectOne(tenantAuthAiLambdaQueryWrapper);
        if(tenantAuthAi != null) {
            TenantAuthAiDto tenantAuthAiDto = new TenantAuthAiDto();
            BeanUtils.copyProperties(tenantAuthAi, tenantAuthAiDto);
            return tenantAuthAiDto;
        }
        return null;
    }

    @Override
    public AjaxResult deleteTenantAuthAi(TenantAuthAiVo tenantAuthAiVo) {
        TenantAuthAiDto tenantAuthAiDto = selectTenantAuthAi(tenantAuthAiVo.getUid());
        if(tenantAuthAiDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<TenantAuthAi> tenantAuthAiLambdaQueryWrapper = new QueryWrapper<TenantAuthAi>().lambda();
        tenantAuthAiLambdaQueryWrapper.eq(TenantAuthAi::getUid, tenantAuthAiVo.getUid());
        tenantAuthAiMapper.delete(tenantAuthAiLambdaQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public TenantAuthAiDto selectTenantAuthAi(String uid) {
        LambdaQueryWrapper<TenantAuthAi> tenantAuthAiLambdaQueryWrapper = new QueryWrapper<TenantAuthAi>().lambda();
        tenantAuthAiLambdaQueryWrapper.eq(TenantAuthAi::getUid, uid);
        TenantAuthAi tenantAuthAi = tenantAuthAiMapper.selectOne(tenantAuthAiLambdaQueryWrapper);
        if(tenantAuthAi != null) {
            TenantAuthAiDto tenantAuthAiDto = new TenantAuthAiDto();
            BeanUtils.copyProperties(tenantAuthAi, tenantAuthAiDto);
            return tenantAuthAiDto;
        }
        return null;
    }
}
